package com.sunday.shoufen.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.common.utils.DeviceUtils;
import com.sunday.common.utils.PixUtils;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.widgets.BetterRecyclerView;
import com.sunday.common.widgets.MyRatingBar;
import com.sunday.common.widgets.banner.ConvenientBanner;
import com.sunday.common.widgets.banner.holder.ViewHolderCreator;
import com.sunday.shoufen.R;
import com.sunday.shoufen.entity.Ad;
import com.sunday.shoufen.entity.BizShop;
import com.sunday.shoufen.entity.IndexClassify;
import com.sunday.shoufen.ui.BizDistrictDetailActivity;
import com.sunday.shoufen.ui.ShopDetailActivity;
import com.sunday.shoufen.ui.ShopListActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.http.Header;

import static android.R.attr.dashGap;
import static android.R.attr.tag;
import static com.sunday.shoufen.base.Constants.TEST_URL;


/**
 * Created by 刘涛 on 2017/10/17.
 */

public class IndexAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private final static int TYPE_HEAD = 0;
    private final static int TYPE_LIST = 1;
    private LayoutInflater layoutInflater;
    private int appWidth;
    private List<IndexClassify> shopCats;
    private List<Ad> adList;
    private List<BizShop> activeShops;
    private List<BizShop> hotShops;


    public IndexAdapter(Context context, List<Ad> ads, List<IndexClassify> shopCats, List<BizShop> activeShops, List<BizShop> hotShops) {
        this.mContext = context;
        layoutInflater = LayoutInflater.from(mContext);
        appWidth = DeviceUtils.getDisplay(mContext).widthPixels;
        this.adList = ads;
        this.shopCats = shopCats;
        this.activeShops = activeShops;
        this.hotShops = hotShops;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEAD;
        } else {
            return TYPE_LIST;
        }
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_HEAD:
                View view = layoutInflater.inflate(R.layout.index_head, null);
                //RecyclerView.LayoutParams layoutParams=new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                //view.setLayoutParams(layoutParams);
                return new HeadHolder(view);
            case TYPE_LIST:
                view = layoutInflater.inflate(R.layout.index_list_item, null);
                //RecyclerView.LayoutParams layoutParams=new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                //view.setLayoutParams(layoutParams);
                return new ListHolder(view);

        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case TYPE_HEAD:
                HeadHolder headHolder = (HeadHolder) holder;
                headHolder.adapter.setDataSet(shopCats);
                headHolder.adapter1.setDataSet(activeShops);
                headHolder.adapter.notifyDataSetChanged();
                headHolder.adapter1.notifyDataSetChanged();
                headHolder.ads.clear();
                headHolder.ads.addAll(adList);
                headHolder.banner.notifyDataSetChanged();
                break;
            case TYPE_LIST:
                ListHolder listHolder = (ListHolder) holder;
                BizShop item = hotShops.get(position - 1);
                Glide.with(mContext)
                        .load(item.getImage())
                        .placeholder(R.mipmap.ic_default)
                        .error(R.mipmap.ic_default)
                        .into(listHolder.shopImg);
                listHolder.shopName.setText(item.getShopName());
                listHolder.shopDesc.setText(StringUtils.listToString(item.getTags()));
                listHolder.shopDistance.setText(item.getDistance());
                listHolder.ratingScore.setText((int) item.getScore() + "分");
                listHolder.ratingBar.setRating((int) item.getScore(), R.drawable.rating_bar, false, 30);
                listHolder.rlShop.setTag(item);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return hotShops.size() + 1;
    }

    class HeadHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @Bind(R.id.banner)
        ConvenientBanner banner;
        @Bind(R.id.recycler_view1)
        BetterRecyclerView recyclerView1;
        @Bind(R.id.ll_check_more_active)
        LinearLayout llCheckMoreActive;
        @Bind(R.id.recycler_view2)
        BetterRecyclerView recyclerView2;
        @Bind(R.id.ll_check_more_shop)
        LinearLayout llCheckMoreShop;

        private LinearLayoutManager layoutManager;
        private LinearLayoutManager layoutManager1;
        private IndexClassifyAdapter adapter;
        private IndexHotShopAdapter adapter1;
        private AdHolder indexAdHolder;
        private List<Ad> ads=new ArrayList<>();


        public HeadHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            layoutManager = new LinearLayoutManager(mContext);
            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            recyclerView1.setLayoutManager(layoutManager);
            recyclerView1.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            adapter = new IndexClassifyAdapter(mContext);
            recyclerView1.setAdapter(adapter);

            layoutManager1 = new LinearLayoutManager(mContext);
            layoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);
            recyclerView2.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            recyclerView2.setLayoutManager(layoutManager1);
            adapter1 = new IndexHotShopAdapter();
            recyclerView2.setAdapter(adapter1);
            indexAdHolder = new AdHolder();
            banner.setPages(new ViewHolderCreator<AdHolder>() {
                @Override
                public AdHolder createHolder() {
                    return indexAdHolder;
                }
            }, ads)
                    .setPageIndicator(new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused});
            banner.startTurning(3000);
            llCheckMoreActive.setOnClickListener(this);
            llCheckMoreShop.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ll_check_more_active:
                    Intent intent = new Intent(mContext, BizDistrictDetailActivity.class);
                    mContext.startActivity(intent);
                    break;
                case R.id.ll_check_more_shop:
                    Intent intent1= new Intent(mContext, ShopListActivity.class);
                    mContext.startActivity(intent1);
                    break;
            }
        }
    }

    class ListHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @Bind(R.id.shop_img)
        ImageView shopImg;
        @Bind(R.id.shop_name)
        TextView shopName;
        @Bind(R.id.rating_bar)
        MyRatingBar ratingBar;
        @Bind(R.id.rating_score)
        TextView ratingScore;
        @Bind(R.id.shop_desc)
        TextView shopDesc;
        @Bind(R.id.shop_distance)
        TextView shopDistance;
        @Bind(R.id.rl_shop)
        LinearLayout rlShop;

        public ListHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            rlShop.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            BizShop item = (BizShop) v.getTag();
            Intent intent = new Intent(mContext, ShopDetailActivity.class);
            intent.putExtra("shopId", item.getShopId());
            intent.putExtra("type", item.getType());
            mContext.startActivity(intent);
        }
    }

    class IndexHotShopAdapter extends RecyclerView.Adapter {

        private List<BizShop> dataSet = new ArrayList<>();

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.list_index_hot_shop, null);
            RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(appWidth * 5 / 6, ViewGroup.LayoutParams.WRAP_CONTENT);
            view.setLayoutParams(layoutParams);
            return new ImgHolder(view);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ImgHolder imgHolder = (ImgHolder) holder;
            BizShop item = dataSet.get(position);
            Glide.with(mContext)
                    .load(item.getImage())
                    .placeholder(R.mipmap.ic_default)
                    .error(R.mipmap.ic_default)
                    .into(imgHolder.imageView);
            imgHolder.imageView.setTag(R.id.image, item);
        }

        @Override
        public int getItemCount() {
            return dataSet.size();
        }

        public void setDataSet(List<BizShop> dataSet) {
            this.dataSet = dataSet;
        }

        class ImgHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
            ImageView imageView;
            LinearLayout layout;


            public ImgHolder(View itemView) {
                super(itemView);
                imageView = (ImageView) itemView.findViewById(R.id.image);
                layout = (LinearLayout) itemView.findViewById(R.id.linearlayout);
                imageView.setOnClickListener(this);
            }

            @Override
            public void onClick(View v) {
                BizShop item = (BizShop) v.getTag(R.id.image);
                Intent intent = new Intent(mContext, ShopDetailActivity.class);
                intent.putExtra("shopId", item.getShopId());
                intent.putExtra("type", item.getType());
                mContext.startActivity(intent);
            }
        }
    }


}
